package lesson12;

import java.io.*;

public class Test01 {
    // 字节输入流
    public void testInputStream(){
        FileInputStream fis = null;
        try {
            // 1.建立流对象
            fis = new FileInputStream("1.txt");
            int n = 0;
            byte [] bs = new byte[1024];
            // 2. 读取数据
            while((n = fis.read(bs)) != -1){
                System.out.print(new String(bs,0,n));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 3. 关闭流
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    // 字节输出流
    public void testOutputStream(){
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("2.txt");
            String str = "你好,java";
            fos.write(str.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 字符输入流
    public void testReader(){
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader("1.txt");
            br = new BufferedReader(fr);
//            int c = 0;
//            while((c = fr.read()) != -1){
//                System.out.print((char)c);
//            }
            String str = "";
            while((str = br.readLine()) != null){
                System.out.println(str);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fr != null){
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    // 对象的序列化：将对象以字节的方式输出
    public void writeObject(Object obj){
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream("obj.dat"));
            oos.writeObject(obj);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(oos != null){
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    // 对象的反序列化：将字节数据转换成对象输入
    public Object readObject(){
        ObjectInputStream ois = null;
        Object obj = null;
        try {
            ois = new ObjectInputStream(new FileInputStream("obj.dat"));
            obj = ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if(ois != null){
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return obj;

    }

    /**
     * 文件拷贝
     * @param source 原文件路径
     * @param dist 目标文件路径
     */
    public void copy(String source,String dist){
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(dist);
            int n = 0;
            byte [] bs = new byte[1024];
            while((n = fis.read(bs)) != -1){
                fos.write(bs,0,n);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fis != null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos != null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static void main(String[] args) {
        Test01 test01 = new Test01();
//        test01.testInputStream();
//        test01.testOutputStream();
//        test01.testReader();
//        Student student = new Student("张三",20,"男");
//        test01.writeObject(student);
//        System.out.println(test01.readObject());
        test01.copy("D:\\Pictures\\icon.png","d:\\ttt.png");

    }


}
